package com.wang.learn.cloudredis.controller;

import com.wang.learn.cloudredis.utils.AliyunOSSUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName RedisController
 * Description
 *
 * @author wang
 * Date 2021/6/19 10:48
 */
@RestController
@RequestMapping("/file")
public class UploadController {


    /**
     * 文件上传
     */
    @PostMapping(value = "/upload")
    public Object upload(@RequestParam("files") List<MultipartFile> files) {
        List<Object> list = new ArrayList<>();
        try {
            for (MultipartFile file : files) {
                String uploadUrl = AliyunOSSUtil.upLoad(file);
                list.add(uploadUrl);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    /**
     * 文件删除
     */
    @PostMapping(value = "/delete")
    public Object delete(@RequestParam("fileUrl") String fileUrl) {
        try {
                AliyunOSSUtil.delete(fileUrl);
        } catch (Exception ex) {
            ex.printStackTrace();
            return "fail";
        }
        return "success";
    }
}
